package com.ybt.pub;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.log4j.Logger;

public class Utils {
    private static Logger logger = Logger.getLogger(Utils.class);

    public static boolean isNull(String value) {
        return value == null ? true : "".equals(value);
    }

    /**
     * 将文件拷贝到某个目录
     * 
     * @param source
     *            源文件
     * @param target
     *            目标文件目录
     * @throws Exception
     */
    public static void copyFile(File source, String target) throws Exception {
        InputStream in = null;
        OutputStream out = null;
        try {
            File file = new File(target);
            if (!file.exists()) {
                file.mkdirs();
            }
            in = new BufferedInputStream(new FileInputStream(source));
            out = new BufferedOutputStream(new FileOutputStream(target
                    + File.separator + source.getName()));
            int c = 0;
            while ((c = in.read()) != -1)
                out.write(c);
            logger.debug("copy file successfully : " + target + File.separator
                    + source.getName());
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (Exception e) {
                    logger.debug(e, e);
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (Exception e) {
                    logger.debug(e, e);
                }
            }
        }
    }

    /**
     * 删除文件
     * @param target
     *            目标文件目录
     */
    public static void deleteFile(String target) {
        File file = new File(target);
        if (file.exists() && file.isFile()) {
            file.deleteOnExit();
        } else {
            logger.warn("can't find the configuration file : " + target);
        }
    }

}
